import {BootMixin} from '@loopback/boot';
import {ApplicationConfig} from '@loopback/core';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication, RestBindings} from '@loopback/rest';
import {
  RestExplorerBindings,
  RestExplorerComponent,
} from '@loopback/rest-explorer';
import {ServiceMixin} from '@loopback/service-proxy';
// import moment from "moment";
import moment from 'moment-timezone';
import path from 'path';
import {MySequence} from './sequence';

// 添加token验证
import {AuthenticationComponent} from '@loopback/authentication';
import {
  JWTAuthenticationComponent,
  RefreshTokenServiceBindings,
  UserServiceBindings
} from '@loopback/authentication-jwt';
import {DbDataSource} from './datasources';


export {ApplicationConfig};

export class Mylb4Application extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
    super(options);

    // Set up the custom sequence
    this.sequence(MySequence);

    // Set up default home page
    this.static('/', path.join(__dirname, '../public'));

    // Customize @loopback/rest-explorer configuration here
    this.configure(RestExplorerBindings.COMPONENT).to({
      path: '/explorer',
    });
    this.component(RestExplorerComponent);

    this.projectRoot = __dirname;
    // Customize @loopback/boot Booter Conventions here
    this.bootOptions = {
      controllers: {
        // Customize ControllerBooter Conventions here
        dirs: ['controllers'],
        extensions: ['.controller.js'],
        nested: true,
      },
    };
    // 抛出错误详情给前端
    this.bind(RestBindings.ERROR_WRITER_OPTIONS).to({debug: true});
    this.configLocaleWeek().configTimezone();

    // ------ ADD SNIPPET AT THE BOTTOM  下面是for token使用 ---------
    // Mount authentication system
    this.component(AuthenticationComponent);
    // Mount jwt component
    this.component(JWTAuthenticationComponent);
    // Bind datasource
    this.dataSource(DbDataSource, UserServiceBindings.DATASOURCE_NAME);
    this.dataSource(DbDataSource, RefreshTokenServiceBindings.DATASOURCE_NAME);
    // this.bind(UserServiceBindings.USER_SERVICE).toClass(MyUserService);
    // Bind datasource for refresh token
    // this.dataSource(TrainingDataSource, RefreshTokenServiceBindings.DATASOURCE_NAME);

    // 刷新token
    // // for jwt access token
    // this.bind(TokenServiceBindings.TOKEN_SECRET).to(TokenServiceConstants.TOKEN_SECRET_VALUE);
    // // for refresh token
    // this.bind(RefreshTokenServiceBindings.REFRESH_SECRET).to(RefreshTokenConstants.REFRESH_SECRET_VALUE);
    ///refresh bindings

    // 尝试
    // this.bind(RefreshTokenServiceBindings.REFRESH_TOKEN_SERVICE).toClass(
    //   RefreshtokenService
    // );
    // //  Refresh token bindings
    // this.bind(RefreshTokenServiceBindings.REFRESH_SECRET).to(
    //   RefreshTokenConstants.REFRESH_SECRET_VALUE
    // )
    // this.bind(RefreshTokenServiceBindings.REFRESH_EXPIRES_IN).to(
    //   RefreshTokenConstants.REFRESH_EXPIRES_IN_VALUE
    // )
    // this.bind(RefreshTokenServiceBindings.REFRESH_ISSUER).to(
    //   RefreshTokenConstants.REFRESH_ISSUER_VALUE
    // )
    // //refresh token repository binding
    // this.bind(RefreshTokenServiceBindings.REFRESH_REPOSITORY).toClass(
    //   RefreshTokenRepository
    // )

    // FIXME： 待修  使用本地数据库
    // Bind user service
    // this.bind(UserServiceBindings.USER_SERVICE).toClass(CustomUserService);
    // // Bind user and credentials repository
    // this.bind(UserServiceBindings.USER_REPOSITORY).toClass(
    //   MyUserRepository,
    // );
    // this.bind(UserServiceBindings.USER_CREDENTIALS_REPOSITORY).toClass(
    //   MyUserCredentialsRepository,
    // )
  }

  /**
   * 时区设置
   * @see: https://www.leangoo.com/kanban/board/go/4165178/4702712140f63566
   * @see: http://momentjs.cn/docs/#/customization/dow-doy/
   * @desciption: 採用ISO 8601
   * @private
   * @memberof ApiApplication
   */
  private configLocaleWeek(): this {
    moment.updateLocale('en', {
      week: {
        dow: 1, // 星期的第一天是星期一
        doy: 4, // 年份的第一周必须包含1月4日 (7 + 1 - 4)
      },
    });
    return this;
  }
  /**
   *
   * @description 设置默认时区
   * @see https://blog.csdn.net/jayhkw/article/details/88956215
   * @private
   * @memberof ApiApplication
   */
  private configTimezone(): this {
    moment.tz.setDefault('Asia/Shanghai');
    return this;
  }
}
